package cn.school.controller.basice;


import cn.school.basics.service.BaseSpaceLocationService;
import cn.school.basics.vo.BaseSpaceLocationVO;
import cn.school.basics.vo.DormitoryBerthVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@RestController
@Api(tags = "Space-Location => 空间位置控制器")
@RequestMapping("/spaceLocation")
public class BaseSpaceLocationController {

    @Autowired
    private BaseSpaceLocationService baseSpaceLocationService;

    @ApiOperation("findBaseSpaceLocationPageList => 分页查询位置信息")
    @PostMapping(value = "/findBaseSpaceLocationPageList")
    public CommonResult<CommonPage<BaseSpaceLocationVO>> findBaseSpaceLocationPageList(@RequestBody BaseSpaceLocationVO vo) {
        IPage<BaseSpaceLocationVO> pageList = baseSpaceLocationService.findBaseSpaceLocationPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("addOrUpdateBaseSpaceLocationInfo => 新增or修改")
    @PostMapping(value = "/addOrUpdateBaseSpaceLocationInfo")
    public CommonResult<CommonPage<BaseSpaceLocationVO>> addOrUpdateBaseSpaceLocationInfo(@RequestBody BaseSpaceLocationVO vo) {
        int num = baseSpaceLocationService.addOrUpdateBaseSpaceLocationInfo(vo);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed("提交失败");
    }

    @ApiOperation("deleteBaseSpaceLocationInfo => 删除")
    @PostMapping(value = "/deleteBaseSpaceLocationInfo")
    public CommonResult<CommonPage<BaseSpaceLocationVO>> deleteBaseSpaceLocationInfo(@RequestBody BaseSpaceLocationVO vo) {
        int num = baseSpaceLocationService.deleteBaseSpaceLocationInfo(vo);
        if (num == -1) {
            return CommonResult.failed("当前位置信息，包含下级不能删除");
        }
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed("提交失败");
    }

    @ApiOperation("locationTypeList => 按类型查询会议室")
    @PostMapping(value = "/locationTypeList")
    public CommonResult<List<BaseSpaceLocationVO>> locationTypeList(@RequestBody BaseSpaceLocationVO vo) {
        List<BaseSpaceLocationVO> list = baseSpaceLocationService.locationTypeList(vo.getLocationType());
        return CommonResult.success(list);
    }

    @ApiOperation("details => 详情")
    @GetMapping(value = "/details/{id}")
    public CommonResult<BaseSpaceLocationVO> locationDetails(@PathVariable String id) {
        BaseSpaceLocationVO vo = baseSpaceLocationService.locationDetails(id);
        return CommonResult.success(vo);
    }

    @ApiOperation("宿舍床位集合")
    @GetMapping(value = "/getDormitoryBerthList/{id}")
    public CommonResult<List<DormitoryBerthVO>> getDormitoryBerthList(@PathVariable String id) {
        List<DormitoryBerthVO> list = baseSpaceLocationService.getDormitoryBerthList(id);
        return CommonResult.success(list);
    }

    @ApiOperation("修改床位")
    @PostMapping(value = "/updateDed")
    public CommonResult<List<DormitoryBerthVO>> updateDedUse(@RequestBody DormitoryBerthVO vo) {
        int num = baseSpaceLocationService.updateDed(vo);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("新增或修改楼栋信息")
    @PostMapping(value = "/buildingAddOrUpdate")
    public CommonResult<BaseSpaceLocationVO> buildingAddOrUpdate(@RequestBody BaseSpaceLocationVO vo) {
        BaseSpaceLocationVO baseSpaceLocationVO = baseSpaceLocationService.buildingAddOrUpdate(vo);
        if (!baseSpaceLocationVO.equals(null)) {
            return CommonResult.success(baseSpaceLocationVO);
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询楼层信息")
    @PostMapping(value = "/buildingSelect")
    public CommonResult<List<BaseSpaceLocationVO>> buildingSelect(@RequestBody BaseSpaceLocationVO vo) {
        if (vo.getId() != null) {
            if (vo.getLocationPid() == 0) {
                List<BaseSpaceLocationVO> details = baseSpaceLocationService.findBuildingSon(vo.getId());
                return CommonResult.success(details);
            } else if (vo.getLocationPid() > 0) {
                List<BaseSpaceLocationVO> details = baseSpaceLocationService.findBuildingSon(vo.getLocationPid());
                return CommonResult.success(details);
            }
        }
        return CommonResult.success();
    }

    @ApiOperation("删除楼层")
    @PostMapping(value = "/buildingDelete")
    public CommonResult<BaseSpaceLocationVO> buildingDelete(@RequestBody BaseSpaceLocationVO vo) {
        List<BaseSpaceLocationVO> buildingSon = baseSpaceLocationService.findBuildingSon(vo.getId());
        int max = buildingSon.stream()
                .mapToInt(location -> Math.toIntExact(location.getId()))
                .max()
                .orElse(0);

        if (baseSpaceLocationService.buildingDelete(max)) {
            return CommonResult.success(vo);
        }
        return CommonResult.failed();
    }

    @ApiOperation("新增教室宿舍")
    @GetMapping(value = "/selectDormitory")
    public CommonResult<List<BaseSpaceLocationVO>> selectDormitory() {
        List<BaseSpaceLocationVO> details = baseSpaceLocationService.findBuildingSon(0L);

        if (details != null) {
            for (BaseSpaceLocationVO detail : details) {
                List<BaseSpaceLocationVO> buildingSon = baseSpaceLocationService.findBuildingSon(detail.getId());
                detail.setBaseSpaceLocationVOList(buildingSon);
            }
            return CommonResult.success(details);
        }
        return CommonResult.failed();
    }
}
